#include "HY1C_out.h"
#include <stdlib.h>
#include <math.h>
#include "l12_proto.h"

static float badval =  BAD_FLT;
static float minval =  0.0;
static float maxval = 10.0*1000;


float conv_rrs_to_555(float Rrs, float wave)
{
    float sw, a1, b1, a2, b2;

    if (fabs(wave-555) > 2) {
      if (fabs(wave-547) <= 2) {
        sw = 0.001723;
        a1 = 0.986;
        b1 = 0.081495;
        a2 = 1.031;
        b2 = 0.000216;
      } else if (fabs(wave-550) <= 2) {
        sw = 0.001597;
        a1 = 0.988;
        b1 = 0.062195;
        a2 = 1.014;
        b2 = 0.000128;
      } else if (fabs(wave-560) <= 2) {
        sw = 0.001148;
        a1 = 1.023;
        b1 = 0.103624;
        a2 = 0.979;
        b2 = 0.000121;
      } else {
        HY1C_out("-E- %s line %d: Unable to convert Rrs at %f to 555nm.\n",__FILE__,__LINE__,wave);
        exit(1);
      }

      if (Rrs < sw)
          Rrs = pow(10.0,a1 * log10(Rrs) - b1);
      else
          Rrs = a2 * Rrs - b2;
    }

    return(Rrs);    
}
